package com.jzo2o.foundations.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.enums.IsHotEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
    private final ServeItemMapper serveItemMapper;
    private final RegionMapper regionMapper;
    private final ServeMapper serveMapper;

    public ServeServiceImpl(ServeItemMapper serveItemMapper, RegionMapper regionMapper, ServeMapper serveMapper) {
        this.serveItemMapper = serveItemMapper;
        this.regionMapper = regionMapper;
        this.serveMapper = serveMapper;
    }

    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        return PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
    }

    /**
     * 批量新增服务
     * @param serveUpsertReqDTOList
     */
    @Override
    @Transactional
    public void batchAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        //检查服务项是否为激活状态，如果不是，则不能添加服务
        for (ServeUpsertReqDTO serveUpsertReqDTO : serveUpsertReqDTOList){
            ServeItem serveItem = serveItemMapper.selectById(serveUpsertReqDTO.getServeItemId());
            if(ObjectUtil.isEmpty(serveItem)||serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
                throw new RuntimeException("服务项未激活，不能添加服务");
            }
            //检查是否重复新增
            Integer count = lambdaQuery().eq(Serve::getRegionId, serveUpsertReqDTO.getRegionId())
                    .eq(Serve::getServeItemId, serveUpsertReqDTO.getServeItemId())
                    .count();
            if(count>0){
                throw new RuntimeException("服务已存在");
            }
            //新增服务
            Serve serve = BeanUtils.toBean(serveUpsertReqDTO, Serve.class);
            //获取城市编码
            Region region = regionMapper.selectById(serveUpsertReqDTO.getRegionId());
            serve.setCityCode(region.getCityCode());
            serveMapper.insert(serve);
        }
    }
    /**
     * 修改服务价格
     * @param id
     * @param price
     */
    @Override
    @Transactional
    public void update(Long id, BigDecimal price){
        Serve serve = serveMapper.selectById(id);
        serve.setPrice(price);
        int i = serveMapper.updateById(serve);
        if(i<=0){
            throw new RuntimeException("更新失败");
        }
        //黑马
//        boolean update = lambdaUpdate()
//                .eq(Serve::getId, id)
//                .set(Serve::getPrice, price)
//                .update();
//        if(!update){
//            throw new CommonException("修改服务价格失败");
//        }
    }

    /**
     * 上架服务
     * @param id
     */
    @Override
    @Transactional
    public void onSale(Long id) {
        //首先查看区域服务当前的状态
        Serve serve = baseMapper.selectById(id);
        if(ObjectUtil.isNull(serve)){
            throw new ForbiddenOperationException("区域服务不存在");
        }
        //上架状态
        Integer saleStatus = serve.getSaleStatus();
        //草稿或下架状态方可上架
        if (!(saleStatus==FoundationStatusEnum.INIT.getStatus() || saleStatus==FoundationStatusEnum.DISABLE.getStatus())) {
            throw new ForbiddenOperationException("草稿或下架状态方可上架");
        }
        //服务项id
        Long serveItemId = serve.getServeItemId();
        ServeItem serveItem = serveItemMapper.selectById(serveItemId);
        if(ObjectUtil.isNull(serveItem)){
            throw new ForbiddenOperationException("所属服务项不存在");
        }
        //服务项的启用状态
        Integer activeStatus = serveItem.getActiveStatus();
        //服务项为启用状态方可上架
        if (!(FoundationStatusEnum.ENABLE.getStatus()==activeStatus)) {
            throw new ForbiddenOperationException("服务项为启用状态方可上架");
        }
        //上架
        serve.setSaleStatus(FoundationStatusEnum.ENABLE.getStatus());
        serveMapper.updateById(serve);

    }

    /**
     * 删除服务
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        //验证区域服务是否存在
        Serve serve = baseMapper.selectById(id);
        if(ObjectUtil.isNull(serve)){
            throw new ForbiddenOperationException("区域服务不存在");
        }
        //判断是否为草稿状态
        if (serve.getSaleStatus()==FoundationStatusEnum.INIT.getStatus()) {
            throw new ForbiddenOperationException("草稿状态,不可删除");
        }
        serveMapper.deleteById(id);
    }

    /**
     * 下架服务
     * @param id
     */
    @Override
    @Transactional
    public void offSale(Long id) {
        //验证区域服务当前状态是否为上架
        Serve serve = serveMapper.selectById(id);
        if(ObjectUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("区域服务不存在");
        }
        if (serve.getSaleStatus()!=FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("当前服务没有上架,不可下架");
        }
        serve.setSaleStatus(FoundationStatusEnum.DISABLE.getStatus());
        serveMapper.updateById(serve);
    }

    @Override
    @Transactional
    public void onHot(Long id) {
        Serve serve = serveMapper.selectById(id);
        if(ObjectUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("区域服务不存在");
        }
        serve.setIsHot(IsHotEnum.ONHOT.getStatus());
        serveMapper.updateById(serve);
    }

    @Override
    @Transactional
    public void offHot(Long id) {
        Serve serve = serveMapper.selectById(id);
        if(ObjectUtil.isEmpty(serve)){
            throw new ForbiddenOperationException("区域服务不存在");
        }
        serve.setIsHot(IsHotEnum.OFFHOT.getStatus());
        serveMapper.updateById(serve);
    }

}
